import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import viteCompression from 'vite-plugin-compression'
import visualizer from 'rollup-plugin-visualizer'

// https://vitejs.dev/config/
export default ({ mode }) => {
	const env = loadEnv(mode, process.cwd()) // 获取.env文件里定义的环境变量

	return defineConfig({
		// 插件
		plugins: [
			vue({
				template: {
					compilerOptions: {
						isCustomElement: (tag) => /^micro-app/.test(tag)
					}
				}
			}),
			viteCompression(),
			visualizer()
		],
		// 设置路径的别名，src直接使用@代替
		resolve: {
			alias: {
				"@": path.resolve(__dirname, "src"),
			},
		},
		base: env.VITE_BASE_URL, //打包路径
		build: {
			outDir: env.VITE_BASE_URL.split('/').filter(item => item)[env.VITE_BASE_URL.split('/').filter(item => item).length - 1]
		},
		server: {
			host: `0.0.0.0`,
			port: 9531,
			open: true,
			proxy: {
				'/ItApp-Common-Auth': {
					target: 'http://10.86.230.108:8083', // sit
					changeOrigin: true,
				},
				'/socket': {
					target: 'http://10.86.130.231:8669//socket',
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/socket/, '')
				},
			}
		}

	});
}
